import { Component, OnInit, OnDestroy } from '@angular/core';
import * as echarts from 'echarts';

import { ChartConfigurationService } from '../../chart-configuration.service';
import { DataSourceService } from '../../data-source.service';
import {
  SpcChartAbnormalJudgement,
  SpcChartAbnormalJudgementForBrokenLine,
  pointDataCollect,
  List,
  SUnormals
} from '../occ-rules/occrules';

@Component({
  selector: 'app-uchart',
  templateUrl: './uchart.component.html',
  styleUrls: ['./uchart.component.less']
})
export class UChartComponent implements OnInit, OnDestroy {
  rules: number = 0;
  interval: number = 0;
  chartViewPoints: number = 0;
  dataPrecision: number = 3;
  chartViewPrecision: number = 2;

  dataSourcePoints: number = 0;
  randomMin: number = 0;
  randomMax: number = 0;
  dataSourceSamplesID: number[] = [];
  dataSourceDefects: number[] = [];
  dataSourceUnits: number[] = [];

  samplesIDArr: number[] = [];
  sampleDefectsArr: number[] = [];
  sampleUnitsArr: number[] = [];
  values: number[] = [];
  sigmaU: number = 0.0;
  sigmaL: number = 0.0;
  averageValue: number = 0.0;
  lcl: number = 0.0;
  ucl: number = 0.0;
  numbersCount: number = 0;
  moveIndexCount: number = 0;
  timer: any;
  XData: number[] = [];
  markPointData: any = [];
  lineChart: any;
  firstTime: boolean = true;

  sigmaUArr: number[] = [];
  sigmaLArr: number[] = [];
  uclArr: number[] = [];
  lclArr: number[] = [];
  sigmaABOverCLArr: number[] = [];
  sigmaBCOverCLArr: number[] = [];
  sigmaABBelowCLArr: number[] = [];
  sigmaBCBelowCLArr: number[] = [];
  canCalculateBaseLineWithAverageSampleSize: boolean = false;

  clDataRecord: number[] = [];
  uclDataRecord: number[] = [];
  lclDataRecord: number[] = [];
  thIndexPointDataCollect: any;
  defectsPointDataCollect: any;
  unitsPointDataCollect: any;
  defectsPerUnitPointDataCollect: any;
  clPointDataCollect: any;
  uclPointDataCollect: any;
  lclPointDataCollect: any;
  tableDataSourceCollection: any = [];
  showDataTable: boolean = false;
  xScroll: number = 100;

  minYAxis: number = 0;
  maxYAxis: number = 0;

  constructor(private chartCfgSrv: ChartConfigurationService, private dataSourceSrv: DataSourceService) {}

  ngOnInit(): void {
    this.loadChartConfig();
    this.drawChart();
    this.timer = setInterval(() => {
      this.drawChart();
    }, this.interval * 1000);
  }

  ngOnDestroy(): void {
    setTimeout(() => {
      clearInterval(this.timer);
    }, this.interval * 1000);
  }

  loadChartConfig(): void {
    this.rules = this.chartCfgSrv.getChartRules();
    this.interval = this.chartCfgSrv.getChartSampleTimeInterval();
    this.chartViewPoints = this.chartCfgSrv.getChartPoints();
    this.dataPrecision = this.chartCfgSrv.getChartDataPrecision();
    this.chartViewPrecision = this.chartCfgSrv.getChartViewPrecision();

    if (this.chartViewPoints > 15) this.xScroll = this.xScroll + Math.ceil((this.chartViewPoints - 15) / 5) * 50;

    this.dataSourcePoints = this.dataSourceSrv.getChartLength(this.chartCfgSrv.getChartType());
    this.randomMin = this.dataSourceSrv.getRandomMin();
    this.randomMax = this.dataSourceSrv.getRandomMax();

    for (var i = 0; i < this.dataSourcePoints; i++) {
      this.dataSourceSamplesID?.push(this.dataSourceSrv.getSampleID(this.chartCfgSrv.getChartType(), i));
      this.dataSourceDefects?.push(this.dataSourceSrv.getSampleResultData(this.chartCfgSrv.getChartType(), i));
      this.dataSourceUnits?.push(this.dataSourceSrv.getSampleSize(this.chartCfgSrv.getChartType(), i));
    }
  }

  getChartAnomalyValues() {
    this.markPointData.length = 0;
    var unormList: List<SUnormals>;
    if (this.canCalculateBaseLineWithAverageSampleSize) {
      unormList = SpcChartAbnormalJudgement(
        this.ucl,
        this.lcl,
        this.averageValue,
        this.sigmaU,
        this.values,
        this.rules,
        this.sigmaL
      ) as any;
    } else {
      unormList = SpcChartAbnormalJudgementForBrokenLine(
        this.uclArr,
        this.lclArr,
        this.averageValue,
        this.sigmaUArr,
        this.values,
        this.rules,
        this.sigmaLArr
      ) as any;
    }
    for (let item of unormList.ToArray()) {
      this.markPointData?.push({ xAxis: item.pointIndex, yAxis: item.value });
    }
  }

  drawChart() {
    this.updateChartValues();
    this.calculateYAxis();
    this.getChartAnomalyValues();
    if (this.showDataTable) {
      this.populateTable();
    }
    if (this.canCalculateBaseLineWithAverageSampleSize) {
      this.initChartWithAveSample();
    } else {
      this.initChart();
    }
  }

  calculateYAxis() {
    if (this.canCalculateBaseLineWithAverageSampleSize) {
      var minYAxis: number = Math.floor(Math.min(this.lcl, Math.min.apply(null, this.values)));
      this.minYAxis = minYAxis < 0 ? 0 : minYAxis;
      var maxYAxis: number = Math.ceil(Math.max(this.ucl, Math.max.apply(null, this.values)));
      this.maxYAxis = maxYAxis;
    } else {
      var minYAxis: number = Math.floor(Math.min(Math.min.apply(null, this.lclArr), Math.min.apply(null, this.values)));
      this.minYAxis = minYAxis < 0 ? 0 : minYAxis;
      var maxYAxis: number = Math.ceil(Math.max(Math.max.apply(null, this.uclArr), Math.max.apply(null, this.values)));
      this.maxYAxis = maxYAxis;
    }
  }

  initChartWithAveSample() {
    const ec = echarts as any;
    var mainContainer: any = document.getElementById('uChart');
    var resizeMainContainer = function () {
      mainContainer.style.width = `${window.innerWidth * 0.85}px`;
    };
    resizeMainContainer();
    if (this.firstTime) {
      this.lineChart = ec.init(mainContainer);
      this.firstTime = false;
    }
    window.addEventListener('resize', () => {
      resizeMainContainer();
      this.lineChart.resize();
    });

    const lineChartOption = {
      title: {
        text: 'SPC U Chart'
      },
      tooltip: {
        trigger: 'axis'
      },
      xAxis: {
        type: 'category',
        data: this.XData,
        axisLine: {
          show: 'true',
          symbol: ['none', 'arrow'],
          lineStyle: {
            type: 'dashed'
          }
        },
        axisLabel: {
          formatter: '{value}',
          align: 'left'
        },
        axisTick: {
          alignWithLabel: true
        }
      },
      yAxis: {
        type: 'value',
        name: 'Defects/Unit',
        min: this.minYAxis,
        max: this.maxYAxis,
        splitLine: {
          show: false,
          lineStyle: {
            type: 'dashed'
          }
        },
        axisLine: {
          show: 'true',
          symbol: ['none', 'arrow'],
          lineStyle: {
            type: 'dashed'
          }
        },
        axisLabel: {
          formatter: '{value}',
          align: 'left'
        },
        nameTextStyle: {
          fontWeight: 600,
          fontSize: 18
        }
      },
      series: [
        {
          data: this.values,
          type: 'line',
          lineStyle: {
            color: '#0000FF'
          },
          markPoint: {
            symbol: 'circle',
            symbolSize: 10,
            itemStyle: {
              color: '#FF5A3B'
            },
            data: this.markPointData
          },
          markLine: {
            symbol: 'none',
            lineStyle: {
              normal: {
                type: 'solid'
              }
            },
            data: [
              {
                yAxis: this.averageValue,
                lineStyle: {
                  color: '#00FF00'
                },
                label: {
                  show: true,
                  formatter: `CL=${this.averageValue.toFixed(this.chartViewPrecision)}`
                }
              },
              {
                yAxis: this.ucl,
                lineStyle: {
                  color: '#FF0000'
                },
                label: {
                  show: true,
                  formatter: `UCL=${this.ucl.toFixed(this.chartViewPrecision)}`
                }
              },
              {
                yAxis: this.lcl,
                lineStyle: {
                  color: '#FF0000'
                },
                label: {
                  show: true,
                  formatter: `LCL=${this.lcl.toFixed(this.chartViewPrecision)}`
                }
              }
            ]
          }
        },
        {
          data: this.uclArr,
          type: 'line',
          step: true,
          showSymbol: false,
          lineStyle: {
            color: '#FF0000'
          }
        },
        {
          data: this.lclArr,
          type: 'line',
          step: true,
          showSymbol: false,
          lineStyle: {
            color: '#FF0000'
          }
        }
      ]
    };

    if (this.rules > 0) {
      var sigmaBCOverCL = {
        yAxis: this.averageValue + this.sigmaU,
        lineStyle: {
          color: '#1890ff',
          type: 'dashed'
        },
        label: {
          show: true,
          formatter: `σ=${(this.averageValue + this.sigmaU).toFixed(this.chartViewPrecision)}`
        }
      };
      lineChartOption.series[0].markLine?.data.push(sigmaBCOverCL);

      var sigmaABOverCL = {
        yAxis: this.averageValue + this.sigmaU * 2,
        lineStyle: {
          color: '#1890ff',
          type: 'dashed'
        },
        label: {
          show: true,
          formatter: `2σ=${(this.averageValue + this.sigmaU * 2).toFixed(this.chartViewPrecision)}`
        }
      };
      lineChartOption.series[0].markLine?.data.push(sigmaABOverCL);

      var sigmaBCBelowCL = {
        yAxis: this.averageValue - this.sigmaL,
        lineStyle: {
          color: '#1890ff',
          type: 'dashed'
        },
        label: {
          show: true,
          formatter: `σ=${(this.averageValue - this.sigmaL).toFixed(this.chartViewPrecision)}`
        }
      };
      lineChartOption.series[0].markLine?.data.push(sigmaBCBelowCL);

      var sigmaABBelowCL = {
        yAxis: this.averageValue - this.sigmaL * 2,
        lineStyle: {
          color: '#1890ff',
          type: 'dashed'
        },
        label: {
          show: true,
          formatter: `2σ=${(this.averageValue - this.sigmaL * 2).toFixed(this.chartViewPrecision)}`
        }
      };
      lineChartOption.series[0].markLine?.data.push(sigmaABBelowCL);

      var sigmaABOverCLArr = {
        data: this.sigmaABOverCLArr,
        type: 'line',
        step: true,
        showSymbol: false,
        lineStyle: {
          color: '#1890ff',
          type: 'dashed'
        }
      };
      lineChartOption.series.push(sigmaABOverCLArr);

      var sigmaBCOverCLArr = {
        data: this.sigmaBCOverCLArr,
        type: 'line',
        step: true,
        showSymbol: false,
        lineStyle: {
          color: '#1890ff',
          type: 'dashed'
        }
      };
      lineChartOption.series.push(sigmaBCOverCLArr);

      var sigmaABBelowCLArr = {
        data: this.sigmaABBelowCLArr,
        type: 'line',
        step: true,
        showSymbol: false,
        lineStyle: {
          color: '#1890ff',
          type: 'dashed'
        }
      };
      lineChartOption.series.push(sigmaABBelowCLArr);

      var sigmaBCBelowCLArr = {
        data: this.sigmaBCBelowCLArr,
        type: 'line',
        step: true,
        showSymbol: false,
        lineStyle: {
          color: '#1890ff',
          type: 'dashed'
        }
      };
      lineChartOption.series.push(sigmaBCBelowCLArr);
    }

    this.lineChart.setOption(lineChartOption);
  }

  calculateBaseLine(): void {
    this.sigmaUArr = this.sampleUnitsArr?.map((item: number) => {
      return Math.sqrt(this.averageValue / item);
    });
    this.lclArr = this.sigmaUArr?.map((item: number) => {
      var result: number = parseFloat((this.averageValue - 3 * item).toFixed(this.dataPrecision));
      if (result < 0.0) {
        result = 0.0;
      }
      return result;
    });
    this.sigmaLArr = this.lclArr?.map((item: number) => {
      return (this.averageValue - item) / 3;
    });
    this.uclArr = this.sigmaUArr?.map((item: number) => {
      return parseFloat((this.averageValue + 3 * item).toFixed(this.dataPrecision));
    });

    this.sigmaABOverCLArr = this.sigmaUArr?.map((item: number) => {
      return parseFloat((this.averageValue + item * 2).toFixed(this.dataPrecision));
    });
    this.sigmaBCOverCLArr = this.sigmaUArr?.map((item: number) => {
      return parseFloat((this.averageValue + item).toFixed(this.dataPrecision));
    });
    this.sigmaABBelowCLArr = this.sigmaLArr?.map((item: number) => {
      return parseFloat((this.averageValue - item * 2).toFixed(this.dataPrecision));
    });
    this.sigmaBCBelowCLArr = this.sigmaLArr?.map((item: number) => {
      return parseFloat((this.averageValue - item).toFixed(this.dataPrecision));
    });

    var valUcl: number | undefined = this.uclArr?.at(this.uclArr?.length - 1);
    if (valUcl != undefined) {
      this.uclDataRecord?.push(valUcl);
      if (this.uclDataRecord?.length > this.chartViewPoints) {
        this.uclDataRecord?.shift();
      }
    }

    var valLcl: number | undefined = this.lclArr?.at(this.lclArr?.length - 1);
    if (valLcl != undefined) {
      this.lclDataRecord?.push(valLcl);
      if (this.lclDataRecord?.length > this.chartViewPoints) {
        this.lclDataRecord?.shift();
      }
    }
  }

  compareSampleSize(averageSampleSize: number): boolean {
    let minSampleSize = Math.min.apply(null, this.sampleUnitsArr);
    let maxSampleSize = Math.max.apply(null, this.sampleUnitsArr);
    if (minSampleSize >= averageSampleSize / 2 && maxSampleSize <= averageSampleSize * 2) {
      return true;
    }
    return false;
  }

  calculateBaseLineWithAverageSampleSize(averageSampleSize: number) {
    this.sigmaU = Math.sqrt(this.averageValue / averageSampleSize);
    this.sigmaL = this.sigmaU;
    this.lcl = parseFloat((this.averageValue - 3 * this.sigmaU).toFixed(this.dataPrecision));
    this.ucl = parseFloat((this.averageValue + 3 * this.sigmaU).toFixed(this.dataPrecision));
    if (this.lcl < 0.0) {
      this.lcl = 0.0;
      this.sigmaL = (this.averageValue - this.lcl) / 3;
    }

    this.uclArr.length = 0;
    this.lclArr.length = 0;
    this.sigmaABOverCLArr.length = 0;
    this.sigmaBCOverCLArr.length = 0;
    this.sigmaABBelowCLArr.length = 0;
    this.sigmaBCBelowCLArr.length = 0;

    this.uclDataRecord?.push(this.ucl);
    if (this.uclDataRecord?.length > this.chartViewPoints) {
      this.uclDataRecord?.shift();
    }
    this.lclDataRecord?.push(this.lcl);
    if (this.lclDataRecord?.length > this.chartViewPoints) {
      this.lclDataRecord?.shift();
    }
  }

  generateRandomData(randomType: number): number {
    return randomType == 0
      ? Math.ceil(Math.random() * (this.randomMax - this.randomMin))
      : parseFloat((Math.random() * this.randomMin).toFixed(this.dataPrecision));
  }

  updateChartValues() {
    var i: number = 0;
    var totalDefects: number = 0.0;
    var totalUnits: number = 0.0;
    this.XData.length = 0;

    var randomDefects: number = 0;
    var randomUnits: number = 0.0;
    if (this.sampleDefectsArr?.length < this.chartViewPoints) {
      if (this.numbersCount < this.dataSourcePoints) {
        this.samplesIDArr?.push(this.dataSourceSamplesID[this.numbersCount]);
        this.sampleDefectsArr?.push(this.dataSourceDefects[this.numbersCount]);
        this.sampleUnitsArr?.push(this.dataSourceUnits[this.numbersCount]);
      } else {
        var randomDefects: number = this.generateRandomData(0);
        var randomUnits: number = this.generateRandomData(1);
        this.sampleDefectsArr?.push(randomDefects);
        this.sampleUnitsArr?.push(randomUnits);
        this.samplesIDArr?.push(this.samplesIDArr[this.numbersCount - 1] + 1);
      }
      this.values?.push(
        parseFloat((this.sampleDefectsArr[this.numbersCount] / this.sampleUnitsArr[this.numbersCount]).toFixed(this.dataPrecision))
      );

      this.numbersCount++;
      totalDefects = this.sampleDefectsArr?.reduce((a: number, b: number) => a + b);
      totalUnits = this.sampleUnitsArr?.reduce((a: number, b: number) => a + b);
      for (i = 0; i < this.chartViewPoints; i++) {
        this.XData?.push(i + 1);
      }
    } else {
      this.moveIndexCount++;
      this.sampleDefectsArr?.shift();
      this.sampleUnitsArr?.shift();
      this.samplesIDArr?.shift();
      this.values?.shift();
      totalDefects = this.sampleDefectsArr?.reduce((a: number, b: number) => a + b);
      totalUnits = this.sampleUnitsArr?.reduce((a: number, b: number) => a + b);
      for (i = 0; i < this.chartViewPoints - 1; i++) {
        this.XData?.push(i + 1 + this.moveIndexCount);
      }
      if (this.numbersCount < this.dataSourcePoints) {
        this.sampleDefectsArr?.push(this.dataSourceDefects[this.numbersCount]);
        this.sampleUnitsArr?.push(this.dataSourceUnits[this.numbersCount]);
        this.samplesIDArr?.push(this.dataSourceSamplesID[this.numbersCount]);
        this.numbersCount++;
      } else {
        var randomDefects: number = this.generateRandomData(0);
        var randomUnits: number = this.generateRandomData(1);
        this.sampleDefectsArr?.push(randomDefects);
        this.sampleUnitsArr?.push(randomUnits);
        this.samplesIDArr?.push(this.samplesIDArr[i - 1] + 1);
      }
      this.values?.push(parseFloat((this.sampleDefectsArr[i] / this.sampleUnitsArr[i]).toFixed(this.dataPrecision)));
      totalDefects += this.sampleDefectsArr[i];
      totalUnits += this.sampleUnitsArr[i];
      this.XData?.push(i + 1 + this.moveIndexCount);
    }

    this.averageValue = totalDefects / totalUnits;
    this.clDataRecord?.push(this.averageValue);
    if (this.clDataRecord?.length > this.chartViewPoints) {
      this.clDataRecord?.shift();
    }

    let averageSampleSize = parseFloat((totalUnits / this.values?.length).toFixed(this.dataPrecision));
    this.canCalculateBaseLineWithAverageSampleSize = this.compareSampleSize(averageSampleSize);
    if (this.canCalculateBaseLineWithAverageSampleSize) {
      this.calculateBaseLineWithAverageSampleSize(averageSampleSize);
    } else {
      this.calculateBaseLine();
    }
  }

  initChart() {
    const ec = echarts as any;
    var mainContainer: any = document.getElementById('uChart');
    var resizeMainContainer = function () {
      mainContainer.style.width = `${window.innerWidth * 0.85}px`;
    };
    resizeMainContainer();
    if (this.firstTime) {
      this.lineChart = ec.init(mainContainer);
      this.firstTime = false;
    }
    window.addEventListener('resize', () => {
      resizeMainContainer();
      this.lineChart.resize();
    });

    const lineChartOption = {
      title: {
        text: 'SPC U Chart'
      },
      tooltip: {
        trigger: 'axis'
      },
      xAxis: {
        type: 'category',
        data: this.XData,
        axisLine: {
          show: 'true',
          symbol: ['none', 'arrow'],
          lineStyle: {
            type: 'dashed'
          }
        },
        axisLabel: {
          formatter: '{value}',
          align: 'left'
        },
        axisTick: {
          alignWithLabel: true
        }
      },
      yAxis: {
        type: 'value',
        name: 'Defects/Unit',
        min: this.minYAxis,
        max: this.maxYAxis,
        splitLine: {
          show: false,
          lineStyle: {
            type: 'dashed'
          }
        },
        axisLine: {
          show: 'true',
          symbol: ['none', 'arrow'],
          lineStyle: {
            type: 'dashed'
          }
        },
        axisLabel: {
          formatter: '{value}',
          align: 'left'
        },
        nameTextStyle: {
          fontWeight: 600,
          fontSize: 18
        }
      },
      series: [
        {
          data: this.values,
          type: 'line',
          lineStyle: {
            color: '#0000FF'
          },
          markPoint: {
            symbol: 'circle',
            symbolSize: 10,
            itemStyle: {
              color: '#FF5A3B'
            },
            data: this.markPointData
          },
          markLine: {
            symbol: 'none',
            lineStyle: {
              normal: {
                type: 'solid'
              }
            },
            data: [
              {
                yAxis: this.averageValue,
                lineStyle: {
                  color: '#00FF00'
                },
                label: {
                  show: true,
                  formatter: `CL=${this.averageValue.toFixed(this.chartViewPrecision)}`
                }
              }
            ]
          }
        },
        {
          data: this.uclArr,
          type: 'line',
          step: true,
          showSymbol: false,
          lineStyle: {
            color: '#FF0000'
          }
        },
        {
          data: this.lclArr,
          type: 'line',
          step: true,
          showSymbol: false,
          lineStyle: {
            color: '#FF0000'
          }
        }
      ]
    };

    if (this.rules > 0) {
      var sigmaABOverCLArr = {
        data: this.sigmaABOverCLArr,
        type: 'line',
        step: true,
        showSymbol: false,
        lineStyle: {
          color: '#1890ff',
          type: 'dashed'
        }
      };
      lineChartOption.series.push(sigmaABOverCLArr);

      var sigmaBCOverCLArr = {
        data: this.sigmaBCOverCLArr,
        type: 'line',
        step: true,
        showSymbol: false,
        lineStyle: {
          color: '#1890ff',
          type: 'dashed'
        }
      };
      lineChartOption.series.push(sigmaBCOverCLArr);

      var sigmaABBelowCLArr = {
        data: this.sigmaABBelowCLArr,
        type: 'line',
        step: true,
        showSymbol: false,
        lineStyle: {
          color: '#1890ff',
          type: 'dashed'
        }
      };
      lineChartOption.series.push(sigmaABBelowCLArr);

      var sigmaBCBelowCLArr = {
        data: this.sigmaBCBelowCLArr,
        type: 'line',
        step: true,
        showSymbol: false,
        lineStyle: {
          color: '#1890ff',
          type: 'dashed'
        }
      };
      lineChartOption.series.push(sigmaBCBelowCLArr);
    }

    this.lineChart.setOption(lineChartOption);
  }

  viewDataTable(): void {
    this.showDataTable = !this.showDataTable;
    if (!this.showDataTable) {
      this.tableDataSourceCollection?.Clear();
      this.tableDataSourceCollection.length = 0;
    }
  }

  populateTable(): void {
    if (this.thIndexPointDataCollect == undefined) {
      this.thIndexPointDataCollect = new pointDataCollect();
      this.thIndexPointDataCollect.attribute = 'thIndex';
    } else {
      this.thIndexPointDataCollect.result.length = 0;
    }

    if (this.defectsPointDataCollect == undefined) {
      this.defectsPointDataCollect = new pointDataCollect();
      this.defectsPointDataCollect.attribute = 'Defects';
    } else {
      this.defectsPointDataCollect.result.length = 0;
    }

    if (this.unitsPointDataCollect == undefined) {
      this.unitsPointDataCollect = new pointDataCollect();
      this.unitsPointDataCollect.attribute = 'Units';
    } else {
      this.unitsPointDataCollect.result.length = 0;
    }

    if (this.defectsPerUnitPointDataCollect == undefined) {
      this.defectsPerUnitPointDataCollect = new pointDataCollect();
      this.defectsPerUnitPointDataCollect.attribute = 'DefectsPerUnit';
    } else {
      this.defectsPerUnitPointDataCollect.result.length = 0;
    }

    if (this.clPointDataCollect == undefined) {
      this.clPointDataCollect = new pointDataCollect();
      this.clPointDataCollect.attribute = 'CL';
    } else {
      this.clPointDataCollect.result.length = 0;
    }

    if (this.uclPointDataCollect == undefined) {
      this.uclPointDataCollect = new pointDataCollect();
      this.uclPointDataCollect.attribute = 'UCL';
    } else {
      this.uclPointDataCollect.result.length = 0;
    }

    if (this.lclPointDataCollect == undefined) {
      this.lclPointDataCollect = new pointDataCollect();
      this.lclPointDataCollect.attribute = 'LCL';
    } else {
      this.lclPointDataCollect.result.length = 0;
    }

    this.samplesIDArr?.forEach((sampleID: number) => this.thIndexPointDataCollect?.result?.push(sampleID));
    this.sampleDefectsArr?.forEach((sampleDefect: number) => this.defectsPointDataCollect?.result?.push(sampleDefect));
    this.sampleUnitsArr?.forEach((sampleSize: number) => this.unitsPointDataCollect?.result?.push(sampleSize));
    this.values?.forEach((value: number) => this.defectsPerUnitPointDataCollect?.result?.push(value));
    this.clDataRecord?.forEach((cl: number) => this.clPointDataCollect?.result?.push(parseFloat(cl.toFixed(this.dataPrecision))));
    this.uclDataRecord?.forEach((ucl: number) => this.uclPointDataCollect?.result?.push(ucl));
    this.lclDataRecord?.forEach((lcl: number) => this.lclPointDataCollect?.result?.push(lcl));

    if (this.tableDataSourceCollection?.length == 0) {
      this.tableDataSourceCollection = new List<pointDataCollect>();
    } else {
      this.tableDataSourceCollection?.Clear();
    }

    this.tableDataSourceCollection?.Add(this.thIndexPointDataCollect);
    this.tableDataSourceCollection?.Add(this.defectsPointDataCollect);
    this.tableDataSourceCollection?.Add(this.unitsPointDataCollect);
    this.tableDataSourceCollection?.Add(this.defectsPerUnitPointDataCollect);
    this.tableDataSourceCollection?.Add(this.clPointDataCollect);
    this.tableDataSourceCollection?.Add(this.uclPointDataCollect);
    this.tableDataSourceCollection?.Add(this.lclPointDataCollect);
  }
}
